import unittest, requests
# 导入数据库类
from Mysql import mysql


class Weidu(unittest.TestCase):
    def setUp(self) -> None:
        print('start case')
        # 初始化数据库类
        self.db = mysql('10.59.9.19', 'yidongceshi', 'Yd123456!', 'mall')

    def tearDown(self) -> None:
        print('end case')
        self.db.close()

    # 登录
    userId = None
    sessionId = None
    phone = None

    def test_Weidu_01(self):
        r1 = requests.post(url='http://10.59.9.18/small/user/v1/login',
                           json={"phone": "15535738398", "pwd": "zjj031613"})
        print(r1.json())
        # 提取userId sessionId
        Weidu.userId = r1.json()['result']['userId']
        Weidu.sessionId = r1.json()['result']['sessionId']
        # 断言 提取phone
        Weidu.phone = r1.json()['result']['phone']
        print(Weidu.phone)
        result = self.db.chaxun(f'select * from user where phone={str(Weidu.phone)}')
        print(result)
        # 提取用户名 进行对比和数据库
        nickName = r1.json()['result']['nickName']
        print(nickName)
        self.assertEqual(nickName, result[0][1])

    # case_001添加新收货地址
    addres_id = None
    def test_Weidu_02(self):
        r3 = requests.post(url='http://10.59.9.18/small/user/verify/v1/addReceiveAddress',
                           headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                           data={"realName": "小婷", "phone": "18185603008",
                                 "address": "北京昌平区南口镇21号", "zipCode": 101077})
        print(r3.json())
        # 断言 查看数据库是否存在新添加的收获地址
        zipCode = 101077
        result = self.db.chaxun(f'select * from receive_address where zip_Code={zipCode}')
        print(result)
        self.assertIn("北京昌平区南口镇21号", result[0][4])
        message = r3.json()['message']
        self.assertIn('添加成功', message)
        # 提出地址id
        Weidu.addres_id = result[0][0]

    # case_002添加新收货地址
    addres_id = None
    message1=None
    def test_Weidu_03(self):
        r3 = requests.post(url='http://10.59.9.18/small/user/verify/v1/addReceiveAddress',
                           headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                           data={"realName": " ", "phone": "18185603008",
                                 "address": "北京昌平区南口镇21号", "zipCode": 101077})
        print(r3.json())
        # 断言
        Weidu.message1=r3.json()['message']
        self.assertIn('添加成功',Weidu.message1)



    addres_id = None
    timestamp = None

    # case_003添加新收货地址
    def test_Weidu_04(self):
        r3 = requests.post(url='http://10.59.9.18/small/user/verify/v1/addReceiveAddress',
                           headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                           data={"phone": "18185603008",
                                 "address": "北京昌平区南口镇21号", "zipCode": 101077})
        print(r3.json())
        # # 断言
        Weidu.timestamp = r3.json()['timestamp']
        self.assertNotEqual('2025-04-26', Weidu.timestamp)

    # case_004添加新收货地址
    message4=None
    def test_Weidu_04(self):
        r4 = requests.post(url='http://10.59.9.18/small/user/verify/v1/addReceiveAddress',
                           headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                           data={"realName": "小婷", "phone": 18185603008,
                                 "address": "北京昌平区南口镇21号", "zipCode": 101077})
        print(r4.json())
        # # 断言
        Weidu.message4 = r4.json()['message']
        self.assertEqual('添加成功', Weidu.message4)

    # case_005添加新收货地址
    message5=None
    def test_Weidu_05(self):
        r5 = requests.post(url='http://10.59.9.18/small/user/verify/v1/addReceiveAddress',
                           headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                           data={"realName": "小婷", "phone": "大西瓜",
                                 "address": "北京昌平区南口镇21号", "zipCode": 101077})
        print(r5.json())
        # # # 断言
        Weidu.message4 = r5.json()['message']
        self.assertEqual('请正确填写手机号', Weidu.message4)

    # case_006添加新收货地址
    message6 = None
    def test_Weidu_06(self):
        r5 = requests.post(url='http://10.59.9.18/small/user/verify/v1/addReceiveAddress',
                           headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                           data={"realName": "小婷", "phone": "AAA",
                                 "address": "北京昌平区南口镇21号", "zipCode": 101077})
        print(r5.json())
        # # # 断言
        Weidu.message4 = r5.json()['message']
        self.assertIn('请正确填写手机号', Weidu.message4)

    # case_007添加新收货地址
    message7 = None
    def test_Weidu_07(self):
        r5 = requests.post(url='http://10.59.9.18/small/user/verify/v1/addReceiveAddress',
                           headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                           data={"realName": "小婷", "phone": "1818560300",
                                 "address": "北京昌平区南口镇21号", "zipCode": 101077})
        print(r5.json())
        # # # 断言
        Weidu.message7 = r5.json()['message']
        self.assertNotEqual('添加成功', Weidu.message7)

    # case_008添加新收货地址
    message8 = None
    def test_Weidu_08(self):
        r5 = requests.post(url='http://10.59.9.18/small/user/verify/v1/addReceiveAddress',
                           headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                           data={"realName": "小婷", "phone": "0111111111",
                                 "address": "北京昌平区南口镇21号", "zipCode": 101077})
        print(r5.json())
        # # # 断言
        Weidu.message7 = r5.json()['message']
        self.assertNotEqual('添加成功', Weidu.message7)

    # case_010添加新收货地址
    message10 = None
    def test_Weidu_10(self):
        r5 = requests.post(url='http://10.59.9.18/small/user/verify/v1/addReceiveAddress',
                           headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                           data={"realName": "小婷", "phone": "18185603009",
                                 "address": "北京昌平区南口镇21号", "zipCode": 101077})
        print(r5.json())
        # # # 断言
        Weidu.message7 = r5.json()['message']
        self.assertIn('添加成功', Weidu.message7)

if __name__ == '__main__':
    unittest.main()